package priv.lirenhe.dissertation.component.crawler_robot.crawler_actions.crawler_client;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import priv.lirenhe.dissertation.component.crawler_robot.crawler_actions.crawler_Param.Headers;
import priv.lirenhe.dissertation.component.crawler_robot.crawler_actions.crawler_Param.Params;


/*
 * 利用HttpClient进行post请求的工具类
 */
public class HttpClientCrawler {
	private Headers headers ;
	private Params params;
	
	public HttpClientCrawler(Headers headers,Params params){
		if(headers != null){
			this.headers = headers;
		}
		if(params != null){
			this.params = params;
		}
	}
	
	public HttpClientCrawler(){
		
	}
	
	/**
	 * 
	 * @param url
	 * @return
	 */
	public String doPost(String url){
		Map<String,String> paramsMap = null;
		Map<String,String> headersMap = null;
		
		if(this.params != null){
			paramsMap = this.params.getParamsMap();
		}
		if(this.headers != null){
			headersMap = this.headers.getHeadersMap();
		}
		
		return doPost(url,paramsMap,headersMap);
	}
	
	/**
	 * 
	 * @param url
	 * @param charset
	 * @return
	 */
	public String doPost(String url,String charset){
		Map<String,String> paramsMap = null;
		Map<String,String> headersMap = null;
		
		if(this.params != null){
			paramsMap = this.params.getParamsMap();
		}
		if(this.headers != null){
			headersMap = this.headers.getHeadersMap();
		}
		
		return doPost(url,paramsMap,headersMap,charset);
	}
	
	/**
	 * doPost
	 * @param url
	 * @param params
	 * @param headers
	 * @return
	 */
	public String doPost(String url,Map<String,String> params,Map<String,String> headers){
		return doPost(url,params,headers,"utf-8");
	}
	
	/**
	 * doPost
	 * @param url
	 * @param params
	 * @param headers
	 * @param charset
	 * @return
	 */
	public String doPost(String url,Map<String,String> params,Map<String,String> headers,String charset){
		HttpClient httpClient = null;
		HttpPost httpPost = null;
		String result = null;
		try{
			httpClient = (HttpClient) new SSLClient();
			httpPost = new HttpPost(url);
			
			if(headers != null){
				for(Map.Entry<String, String> entry:headers.entrySet()){
					if(!httpPost.containsHeader(entry.getKey())){
						httpPost.setHeader(entry.getKey(),entry.getValue());
					}
				}
			}
			
//			httpPost.addHeader("Accept-Encoding", "gzip, deflate");
//			
//			httpPost.addHeader("Host","www.sy2y.cn");
//			httpPost.addHeader("Accept","*/*");//application/json, text/javascript, */*; q=0.01
//			httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
//			httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//			httpPost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0");
//			httpPost.addHeader("Cookie", "ASP.NET_SessionId=wogxpt55ns4lknia0d40ttzz; CheckCode=0415");
//			httpPost.addHeader("X-Requested-With", "XMLHttpRequest");
//			httpPost.addHeader("Referer", "http://www.sy2y.cn/news.aspx?id=33");
//			httpPost.addHeader("X-Requested-With", "XMLHttpRequest");
			
			
			//设置参数
			if(params != null){
				List<NameValuePair> list = new ArrayList<NameValuePair>();
				Iterator iterator = params.entrySet().iterator();
				while(iterator.hasNext()){
					Entry<String,String> elem = (Entry<String, String>) iterator.next();
					list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
				}
				if(list.size() > 0){
					UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
					httpPost.setEntity(entity);
				}
			}
			
			HttpResponse response = httpClient.execute(httpPost);
			if(response != null){
				HttpEntity resEntity = response.getEntity();
				if(resEntity != null){
					Header contentEncodingHeader = resEntity.getContentEncoding();
					if(contentEncodingHeader!=null && contentEncodingHeader.getValue().equals("gzip")){
						System.out.println("内容编码"+contentEncodingHeader);
						GzipDecompressingEntity gzipEntity = new GzipDecompressingEntity(resEntity);
						result = EntityUtils.toString(gzipEntity,charset);
						return result;
					}else{
						result = EntityUtils.toString(resEntity,charset);
					}
				}
			}
		}catch(Exception ex){
			System.out.println("爬虫出错的url="+url+"输出到exception_url.txt中");
			File file = null;
			BufferedWriter bw = null;
			try {
				file = new File("C:\\Users\\lenovo\\Desktop\\html-jsp\\exception_url.txt");//修改:
				bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
				bw.newLine();
				bw.write(url);
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				if(bw != null){
					try {
						bw.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			ex.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * @param url
	 * @return
	 */
	public String doGet(String url){
		
		System.out.println("doGet url="+url);
		
		Map<String,String> paramsMap = null;
		Map<String,String> headersMap = null;
		
		if(this.params != null){
			paramsMap = this.params.getParamsMap();
		}
		if(this.headers != null){
			headersMap = this.headers.getHeadersMap();
		}
		
		return doGet(url,paramsMap,headersMap);
	}
	
	/**
	 * 
	 * @param url
	 * @param charset
	 * @return
	 */
	public String doGet(String url,String charset){
		Map<String,String> paramsMap = null;
		Map<String,String> headersMap = null;
		
		if(this.params != null){
			paramsMap = this.params.getParamsMap();
		}
		if(this.headers != null){
			headersMap = this.headers.getHeadersMap();
		}
		
		return doGet(url,paramsMap,headersMap,charset);
	}
	
	/**
	 * doGet
	 * @param url
	 * @param params
	 * @param headers
	 * @return
	 */
	public String doGet(String url,Map<String,String> params,Map<String,String> headers){
		return doGet(url,params,headers,"utf-8");
		
	}
	/**
	 * 
	 * @param url
	 * @param params
	 * @param headers
	 * @param charset
	 * @return
	 */
	public String doGet(String url,Map<String,String> params,Map<String,String> headers,String charset){
		HttpClient httpClient = null;
		HttpGet httpGet = null;
		String result = null;
		try{
			httpClient = (HttpClient) new SSLClient();
			//设置参数
			if(params !=null ){
				List<NameValuePair> list = new ArrayList<NameValuePair>();
				Iterator iterator = params.entrySet().iterator();
				while(iterator.hasNext()){
					Entry<String,String> elem = (Entry<String, String>) iterator.next();
					list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
				}
				if(list.size() > 0){
					boolean firstFor = true;
					for(NameValuePair nameValuePair:list){
						if(firstFor){
							url = url + "?" + nameValuePair.getName() + "=" +nameValuePair.getValue() + "&";
							firstFor = false;
						}else{
							url = url + nameValuePair.getName() + "=" +nameValuePair.getValue() + "&";
						}
					}
				}
			}
			
			httpGet = new HttpGet(url);
			
			if(headers != null){
				for(Map.Entry<String, String> entry:headers.entrySet()){
					if(!httpGet.containsHeader(entry.getKey())){
						httpGet.setHeader(entry.getKey(),entry.getValue());
					}
				}
			}
			
			HttpResponse response = httpClient.execute(httpGet);
			
			if(response != null){
				HttpEntity resEntity = response.getEntity();
				if(resEntity != null){
					Header contentEncodingHeader = resEntity.getContentEncoding();
					if(contentEncodingHeader != null){
						if(contentEncodingHeader.getValue().equals("gzip")){
							System.out.println("内容编码"+contentEncodingHeader);
							GzipDecompressingEntity gzipEntity = new GzipDecompressingEntity(resEntity);
							if(charset != null){
								result = EntityUtils.toString(gzipEntity,charset);//charset
							}else{
								result = EntityUtils.toString(gzipEntity);//charset
							}
							return result;
						}
					}else{
						if(charset != null){
							result = EntityUtils.toString(resEntity,charset);//charset
						}else{
							result = EntityUtils.toString(resEntity);//charset
						}
					}
				}
			}
		}catch(Exception ex){
			System.out.println("爬虫出错的url="+url+"输出到exception_url.txt中");
			File file = null;
			BufferedWriter bw = null;
			try {
				file = new File("C:\\Users\\lenovo\\Desktop\\html-jsp\\exception_url.txt");//修改:
				bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
				bw.newLine();
				bw.write(url);
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				if(bw != null){
					try {
						bw.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			ex.printStackTrace();
		}
		return result;
	}
}

/*
String regex = "([\\u3007\\u3400-\\u4DB5\\u4E00-\\u9FCB\\uE815-\\uE864]|[\\uD840-\\uD87F][\\uDC00-\\uDFFF])";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(webContent);
if(matcher.find()){
	character = matcher.group();
}
*/
/*
UniversalDetector detector = new UniversalDetector(null);
//开始给一部分数据，让学习一下啊，官方建议是1000个byte左右（当然这1000个byte你得包含中文之类的）
detector.handleData(character.getBytes(), 0, character.getBytes().length);
//识别结束必须调用这个方法
detector.dataEnd();
//神奇的时刻就在这个方法了，返回字符集编码。
charset = detector.getDetectedCharset();
System.out.println("charset="+charset);
*/
/*
InputStream resInputStream = resEntity.getContent();
File file = new File("C:\\Users\\User\\Desktop\\file.txt");
OutputStream os = new FileOutputStream(file);
byte[] bytes = new byte[1024];
System.out.println("aasdfasdf");
int temp = 0;
while((temp = resInputStream.read(bytes))!=-1){
	os.write(bytes,0,temp);
}
os.close();
*/


/*
System.out.println(resEntity.getContentType());
InputStream is = resEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String buffer = null;
while((buffer = br.readLine())!=null){
	sb.append(buffer);
}
result = sb.toString();
System.out.println(CharSetConverter.getEncoding(result));
byte[] bytes = result.getBytes("iso-8859-1");
result = new String(bytes,"UTF-8");
*/
